package com.swa99ylum.pxp.a.a005;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class IndexerTest {

	public static void main(String[] args) {
		/**
		 * cellList 就好比 xlsx 中的 row，
		 * 1、但是 cellList 是有界的
		 * 2、而 row 是 无界的
		 */
		List<Cell> cellList = Arrays.asList(
				new Cell(0, 1), new Cell(1, 1), new Cell(2, 1), new Cell(3, 1),
				new Cell(4, 0), 
				new Cell(5, 1), new Cell(6, 1), new Cell(7, 1), new Cell(8, 1), new Cell(9, 1), new Cell(10, 1),
				new Cell(11, 0), new Cell(12, 0),
				new Cell(13, 1), new Cell(14, 1), new Cell(15, 1)
				);
		
		Index index = new Index(0);
		Indexer indexer1 = new Indexer(index);
		Indexer indexer2 = new Indexer(index);
		Indexer indexer3 = new Indexer(index);
		
		List<Cell> list1 = new ArrayList<Cell>();
		List<Cell> list2 = new ArrayList<Cell>();
		List<Cell> list3 = new ArrayList<Cell>();
		
		ArrayList<List<Cell>> lists = new ArrayList<List<Cell>>(Arrays.asList(list1, list2, list3));
		ArrayList<Indexer> indexers = new ArrayList<Indexer>(Arrays.asList(indexer1, indexer2, indexer3));
		
		for(int i=0; i<indexers.size(); i++) {
			
			List<Cell> list = lists.get(i);
			Indexer indexer = indexers.get(i);
			
			boolean started = false;
			
			while(true) {
				int currentIndex = indexer.getIndex().increment();
				if(currentIndex == cellList.size()) break;
				
				Cell currentCell = cellList.get(currentIndex);
				
				System.out.println("==========> " + indexer.getIndex().getValue());
				
				if(!started) {
					if(indexer.triggerBeginning(currentCell)) {//开头
						System.out.println("开头啦~~~，将这个 cell 加入 list");
						
						list.add(currentCell);
						started = true;
						
						continue;
						
					} else {//开不了头
						System.out.println("诶，还是 开不了头");
						
						continue;
					}
				}
				
				if(!indexer.triggerEnding(currentCell)) {//结不了尾，仍旧需要 将这个 cell 加入 list
					System.out.println("结不了尾，仍旧需要 将这个 cell 加入 list");
					list.add(currentCell);
					
				}else {//结尾了，跳出 无限循环，不再 拿 cell 了
					System.out.println("结尾啦~~~~");
					started = false;
					break;
				}
			}
			System.out.println(list);
			
//			Cell[] result = (Cell[]) list.toArray(new Cell[0]);
//			System.out.println(Arrays.toString(result));
		}
	}
}
